package com.example.onlinetaxi.view.Adapter;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.onlinetaxi.R;
import com.example.onlinetaxi.callback.OrderCallBack.CompletedOrderCallBack;
import com.example.onlinetaxi.dto.OrderDto.CompletedOrderDto;
import com.example.onlinetaxi.util.TimeUtil;

import java.util.List;

// 继承自RecyclerView.Adapter实现类，泛型是其内部类
public class CancelOrderAdapter extends RecyclerView.Adapter<CancelOrderAdapter.ViewHolder> {

    // 用于构造页面的订单列表
    private List<CompletedOrderDto> cancelOrderList;
    // 删除订单接口
    private CompletedOrderCallBack completedOrderCallBack;

    public static Drawable drawable1;
    public static Drawable drawable2;

    // 构造函数
    public CancelOrderAdapter(List<CompletedOrderDto> cancelOrderList) {
        this.cancelOrderList = cancelOrderList;
    }

    /**
     * 内部类也继承其内部类，并重写构造器
     */
    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView cancelOrderId;
        TextView cancelOrderUseCarTime;
        // 订单类型（实时单/预约单）
        TextView cancelOrderLeftColor;
        TextView cancelOrderRightInfo;
        TextView cancelOrderUseCarType;
        TextView cancelOrderStartInfo;
        TextView cancelOrderEndInfo;
        TextView cancelOrderTime;
        TextView cancelOrderDistance;
        TextView cancelOrderCost;
        TextView cancelOrderCreateTime;
        // 按钮
        Button cancelOrderBtn;

        /**
         * 内部类构造器
         *
         * @param view Recycler子项的最外层布局
         */
        public ViewHolder(View view) {
            super(view);
            // 文本框
            cancelOrderLeftColor = view.findViewById(R.id.cancel_order_left_color);
            cancelOrderRightInfo = view.findViewById(R.id.cancel_order_right_info);
            cancelOrderId = view.findViewById(R.id.cancel_order_id);
            cancelOrderUseCarTime = view.findViewById(R.id.cancel_order_use_car_time);
            cancelOrderUseCarType = view.findViewById(R.id.cancel_order_use_car_type);
            cancelOrderStartInfo = view.findViewById(R.id.cancel_order_start_info);
            cancelOrderEndInfo = view.findViewById(R.id.cancel_order_end_info);
            cancelOrderTime = view.findViewById(R.id.cancel_order_time);
            cancelOrderDistance = view.findViewById(R.id.cancel_order_distance);
            cancelOrderCost = view.findViewById(R.id.cancel_order_cost);
            cancelOrderCreateTime = view.findViewById(R.id.cancel_order_create_time);
            cancelOrderBtn = view.findViewById(R.id.cancel_order_cancel_btn);
        }
    }

    // 第二个被回调，创建一个ViewHolder对象返回
    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        // 加载布局
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.order_item_cancel, parent, false);
        // 布局传递到内部类的构造函数中
        ViewHolder holder = new ViewHolder(view);
        // 返回内部类对象
        return holder;
    }

    // 子项数据赋值，通过这个方法实现滑动添加，第三个被回调
    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        // 得到当前order
        CompletedOrderDto order = cancelOrderList.get(position);

        // 按钮事件
        holder.cancelOrderBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                completedOrderCallBack.clickDeleteButton(order);
            }
        });
        // 设置订单id
        holder.cancelOrderId.setText("订单id:" + order.getId());
        holder.cancelOrderUseCarTime.setText("用车时间: " + TimeUtil.DateToString(order.getUseCarTime()));
        holder.cancelOrderUseCarType.setText(order.getUseCarType());
        holder.cancelOrderStartInfo.setText(order.getStartInfo());
        holder.cancelOrderEndInfo.setText(order.getEndInfo());
        holder.cancelOrderTime.setText("预计乘车时间:" + order.getTime() + "分钟");
        holder.cancelOrderDistance.setText("预计乘车里程:" + order.getDistance() + "公里");
        holder.cancelOrderCreateTime.setText("订单创建时间: " + TimeUtil.DateToString(order.getCreateTime()));

        if (order.getCancelState() == null) {
            holder.cancelOrderRightInfo.setText("已取消");
            holder.cancelOrderRightInfo.setTextColor(Color.parseColor("#e85d30"));
            holder.cancelOrderLeftColor.setBackground(drawable2);
            holder.cancelOrderCost.setText("预计费用：" + order.getCost() + " 元");
        } else {
            if (order.getCancelState().equals("2")) {
                holder.cancelOrderRightInfo.setText("已撤销");
                holder.cancelOrderRightInfo.setTextColor(Color.parseColor("#ff5153"));
                holder.cancelOrderLeftColor.setBackground(drawable1);
                if (order.getCancelReason().equals("")) {
                    order.setCancelReason("无");
                }
                holder.cancelOrderCost.setText("撤销原因：" + order.getCancelReason());
            }
        }
    }

    //  @return 获取长度，第一个被回调
    public int getItemCount() {
        return cancelOrderList.size();
    }

    public void setCompletedOrderCallBack(CompletedOrderCallBack completedOrderCallBack) {
        this.completedOrderCallBack = completedOrderCallBack;
    }
}
